package com.get.backstage.flowable.controller;

import com.get.backstage.common.result.R;
import com.get.backstage.flowable.entity.TaskEntity;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.IdentityService;
import org.flowable.engine.ProcessEngine;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.idm.api.Group;
import org.flowable.idm.api.GroupQuery;
import org.flowable.idm.api.User;
import org.flowable.idm.api.UserQuery;
import org.flowable.task.api.Task;
import org.flowable.task.api.TaskQuery;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 */
@Api(tags = "用户的工作流操作")
@RestController
@RequestMapping("/flow")
@Slf4j
public class FlowController {

    @Autowired
    ProcessEngine processEngine;


    /**
     * 查看组任务
     */
    @RequestMapping("/group/task")
    public R queryGroupTask(String username) throws IOException {


        List<Task> taskList = processEngine.getTaskService().createTaskQuery()
                .taskCandidateUser(username)
                .orderByTaskCreateTime().asc()
                .list();
        ArrayList<TaskEntity> taskEntities = new ArrayList<>();
        for (Task task : taskList){
            TaskEntity taskEntity = new TaskEntity();
            taskEntity.setId(task.getId());
            taskEntity.setName(task.getName());
            taskEntity.setProcessDefinitionId(task.getProcessDefinitionId());
            taskEntity.setProcessInstanceId(task.getProcessInstanceId());
            taskEntity.setExecutionId(task.getExecutionId());
            taskEntity.setTaskDefinitionKey(task.getTaskDefinitionKey());
            taskEntities.add(taskEntity);

        }


        return R.success(taskEntities);
    }


    /**
     * 完成任务
     */
    @RequestMapping("/complete")
    public R processIn(String taskId) throws IOException {
        TaskService taskService = processEngine.getTaskService();
        taskService.complete(taskId);

        return R.success("完成任务");

    }















}
